What is @babel/plugin-transform-react-pure-annotations?
The @babel/plugin-transform-react-pure-annotations package is a Babel plugin designed to enhance optimization for React components by automatically annotating React components with `/*#__PURE__*/` comments. These comments signal to JavaScript bundlers and minifiers, such as Terser, that the component functions can be safely tree-shaken if they are not used. This can lead to smaller bundle sizes in production builds.
What are @babel/plugin-transform-react-pure-annotations's main functionalities?
Automatic Pure Annotation for Functional Components
Automatically adds pure annotations to functional React components, indicating that they can be safely removed if unused.
Before:
const MyComponent = () => <div>Hello World</div>;
After:
const MyComponent = /*#__PURE__*/() => <div>Hello World</div>;
Automatic Pure Annotation for Class Components
Automatically adds pure annotations to the return value of render methods in class components, aiding in their potential tree-shaking.
Before:
class MyComponent extends React.Component {
render() {
return <div>Hello World</div>;
}
}
After:
class MyComponent extends React.Component {
render() {
return /*#__PURE__*/<div>Hello World</div>;
}
}
Other packages similar to @babel/plugin-transform-react-pure-annotations
@babel/plugin-transform-react-inline-elements
Transforms React.createElement calls to object literals. This can improve performance by reducing the overhead of these calls at runtime. However, it does not specifically focus on tree-shaking like @babel/plugin-transform-react-pure-annotations does.
@babel/plugin-transform-react-constant-elements
Hoists element creation to the highest scope possible. This can improve performance in React applications by reducing the need to recreate the same elements on each render. While it optimizes performance, its approach is different from adding pure annotations for tree-shaking.
@babel/plugin-transform-react-pure-annotations
Mark top-level React method calls as pure for tree shaking
See our website @babel/plugin-transform-react-pure-annotations for more information.
Install
Using npm:
npm install --save-dev @babel/plugin-transform-react-pure-annotations
or using yarn:
yarn add @babel/plugin-transform-react-pure-annotations --dev